import {
	getDateString
} from "@/assets/js/common.js";
let Controller = {
	mounted() {},
	activated() {
		this.requestTableList();
		this.getDowmList();
	},
	methods: {
		onTableExport() {
			const arr = this.Model.selectionList;
			if (arr.length == 0) {
				this.$message.warning('请勾选需要导出的数据！')
				return;
			};
			const param = {
				name: 'purchaseStorage',
				idList: arr
			};
			this.$ajax({
				method: 'post',
				url: this.apiTest + `/supplier/common/exportExcel`,
				data: param,
				responseType: 'arraybuffer'
			}).then(res => {
				const data = res.data;
				let blob = new Blob([res.data], {
					type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
				});
				let objectUrl = URL.createObjectURL(blob);
				window.open(objectUrl);
			})
			/* this.$ajax.post(this.apiTest + `/supplier/common/exportExcel`,param)
			  .then(res => {
			    const data = res.data;
			    if(data.status == 200){
			      this.downloadExcel();
			    }
			  }).catch(err => {
			    this.$message.error('导出失败!')
			  }) */
		},
		downloadExcel() {
			this.$ajax({
				metho
			})
		},
		onVoid(whWarehouseId) {
			this.$confirm('是否确认作废?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				const param = {
					whWarehouseId,
					status: 60
				};
				if (param) {
					this.$ajax.post(this.apiTest + `/supplier/whWarehouse/cancelWarehouse`, param)
						.then(res => {
							const data = res.data;
							if (data.status == 200) {
								this.$message({
									type: 'success',
									message: '作废操作成功！'
								});
								this.requestTableList();
							} else {
								this.$message.error(data.msg);
							}
						}).catch(err => {
							this.$message.error('未知错误，请联系管理员');
						})
				}
			}).catch(err => {})
		},
		onWarehousing(row) {
			const whWarehouseId = row.id;
			const inQuantity = row.inQuantity;
			this.$confirm(`本次入库数量( ${inQuantity} )件，是否确认入库?`, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				const param = {
					status: 20,
					whWarehouseId
				};
				if (param) {
					this.$ajax.post(this.apiTest + `/supplier/whWarehouse/updateEntity`, param)
						.then(res => {
							const data = res.data;
							if (data.status == 200) {
								this.$message({
									type: 'success',
									message: '入库操作成功！'
								});
								this.requestTableList();
							} else {
								this.$message.error(data.msg);
							}
						}).catch(err => {
							this.$message.error('未知错误，请联系管理员');
						})
				}
			}).catch(err => {})
		},
		getDowmList() {
			this.$ajax.post(this.apiTest + `/supplier/whWarehouse/getDropBox/204`, {})
				.then(res => {
					const data = res.data;
					if (data.status == 200) {
						const result = data.result;
						this.Model.filterOptionData = result;
						let locationArr = [],
							wareHouseStatusArr = [],
							wareHouseTypeArr = [],
							supplierArr = [];
						for (let i in result.location) { //入库仓库
							locationArr.push({
								label: result.location[i],
								value: i
							});
						};
						for (let i in result.wareHouseStatus) { //入库单状态
							wareHouseStatusArr.push({
								label: result.wareHouseStatus[i],
								value: i
							});
						};
						for (let i in result.wareHouseType) { //入库单类型
							wareHouseTypeArr.push({
								label: result.wareHouseType[i],
								value: i
							});
						};
						for (let i in result.supplier) { //入库单类型
							supplierArr.push({
								label: result.supplier[i],
								value: i
							});
						};
						this.Model.filterOption.items[4].data = supplierArr; //供应商
						this.Model.filterOption.items[3].data = locationArr; //入库仓库
						this.Model.filterOption.items[2].data = wareHouseStatusArr; //入库单状态
						this.Model.filterOption.items[1].data = wareHouseTypeArr; //入库单类型
						this.Model.defaultFilterOption = _.cloneDeep(this.Model.filterOption)
					}
				})
		},
		requestTableList() {
			//请求主表数据
			const params = {
				condition: {
					...this.getSearchInfoParam(),
					countType: 2,
					inventoryDirection: 1
				},
				...this.Model.pager.params,
			};
			this.$ajax
				.post(`/api/whInventoryMain/list`, params)
				// .post(this.apiTest + `/supplier/whInventoryMain/list`, params)
				.then(response => {
					const data = response.data;
					this.Model.tableData = data.result.result;
					this.Model.pager.pageTotal = data.result.total;
					this.Model.pager.pageIndex = data.result.pageIndex;
				})
				.catch(error => {
					this.$message.error('请求错误');
				});
		},
		getSearchInfoParam() {
			//编辑高级搜索时间格式
			let data = _.cloneDeep(this.Model.searchFromData);
			if (data.inventoryStart) {
				data.inventoryStartTime = getDateString(data.inventoryStart[0]);
				data.inventoryStartToTime = getDateString(data.inventoryStart[1]);
				data.inventoryStart = undefined;
			}
			if (data.inventoryEnd) {
				data.inventoryEndTime = getDateString(data.inventoryEnd[0]);
				data.inventoryEndToTime = getDateString(data.inventoryEnd[1]);
				data.inventoryEnd = undefined;
			}
			if (data.create) {
				data.createTime = getDateString(data.create[0]);
				data.createToTime = getDateString(data.create[1]);
				data.create = undefined;
			}
			if (data.submitInventory) {
				data.submitInventoryTime = getDateString(data.submitInventory[0]);
				data.submitInventoryToTime = getDateString(data.submitInventory[1]);
				data.submitInventory = undefined;
			}
			return data;
		},
		tableDetail(row,flag,status) {
			//跳转入库详情
			const name = 'materialInventoryDetail';
			this.$router.push({
				name: name,
				query: {
					data: JSON.stringify({
						id:row.id,
                        flag,
                        status
					})
				}
			})
        },
        //结束盘点
        endInventory(row,that,type) {
            if(type == '结束盘点'){
                this.$confirm('结束盘点后会生成盘点结果，无法再对该盘点进行修改,是否确定结束该盘点任务？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                }).then(() => {
                    this.endInventoryAjax(row.id,3);
                }).catch(() => {
                    that.tips(that,'warning','取消成功')     
                });
            }else{
                this.$confirm('是否确定反审核？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                }).then(() => {
                    this.endInventoryAjax(row.id,2);
                }).catch(() => {
                    that.tips(that,'warning','取消成功')     
                });
            }
            
        },
        //结束、反审核请求
        endInventoryAjax(id,status) {
            var that = this;
            this.$ajax({
                method: 'put',
                //url:this.apiTest+'/supplier/whInventoryMain',
                url:'/api/whInventoryMain',
                data:{
                    id:id,
                    inventoryStatus:status
                }
            }).then((res)=>{
                var data = res.data;
                if(data.status == 200){
                    that.tips(that,'success',data.result);
                    that.getAllShopList(this);
                }else{
                    that.tips(that,'warning',data.msg)
                }
            
            });
        },
        //提交报告
        submitReport(row) {
            var submitter = 1;
            var MainId = row.id;
			this.$ajax({
				method: 'post',
				//url:this.apiTest+`/supplier/whInventoryDetails/submitInventoryReport/${MainId}/${submitter}`,
				url:`/api/whInventoryDetails/submitInventoryReport/${MainId}/${submitter}`
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
                    this.tips(this,'success','提交报告成功')
				}	
			});
        },
		onTableSelectionChange(val) {
			//表格多选操作
			if (val.length == 0) {
				this.Model.selectionList = [];
				return;
			}
			let arr = val.map((item, index, list) => {
				return item.id
			})
			this.Model.selectionList = arr;
		},
		onTableCreate() {
			this.$confirm('盘点库位为包材仓，请悉知！', '盘点任务', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				const param = {
					"createBy ": 131313, //创建人
					"factory": "DC01", //盘点地点    
					"inventoryLocation": "A001,A002", //盘点库位 - 写死包材仓
					"countType": 2, //盘点物品类型 1.商品盘点 2.包材盘点 3.抽盘
					"inventoryDirection": 1 //盘点类型 1.门店  2.供应链
				};
				this.$ajax.post(`/api/whInventoryMain/add`, param)
					.then(res => {
						const data = res.data;
						if (data.status == 200) {
							console.log(data)
						}else{
							this.$message.error(data.msg)
						}
					}).catch(err => {})
			}).catch(() => {});
		},
		onTableCurrentChange(val) {
			//表格分页-当前页变动操作
			this.Model.pager.params.pageIndex = val;
			this.requestTableList();
		},
		onTableSizeChange(val) {
			//表格分页-页数量变动操作
			this.Model.pager.params.pageSize = val;
			this.requestTableList();
		},
		onCardClick(color) {
			//页面card搜索点击操作
			this.Model.cardSelected = color;
		},
		getFilterData(val, item, option) {
			/* 
			 saleOrderStatus订单状态[1]
			 saleOrderTypes售中类型-状态联动[2][3]
			 saleOrderChannels销售渠道[4][5]
			 deliveryTypes发货方式[6]
			 */
			// this.Model.filterOption.items = _.cloneDeep(option);
			for (let i = 0; i < 5; i++) {
				this.Model.filterOption.items[i] = option[i];
			}
			switch (item.key) {
				case 'saleOrderChannels':
					this.Model.filterOption.items[5].data = this.Model.saleManList[item.val].map(obj => {
						return {
							label: obj.value,
							value: obj.key
						}
					});
					this.Model.filterOption.items[5].value = undefined;
					break;
				case 'saleOrderTypes':
					this.Model.filterOption.items[3].data = this.Model.saleAfterOrderStatus[item.val].map(obj => {
						return {
							label: obj.value,
							value: obj.key
						}
					});
					this.Model.filterOption.items[3].value = undefined;
					break;
				default:
					break;
			};
			this.Model.searchFromData = val;
		},
		onFilterSearch(val) {
			this.requestTableList();
		},
		onFilterClear(val) {
			this.Model.searchFromData = _.cloneDeep(this.Model.defaultSearchFromData);
			this.Model.filterOption = _.cloneDeep(this.Model.defaultFilterOption);
		},
		onFilterExport(val) {},
		onTabChange(val) {
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		setPage(pageIndex, pageSize, pageTotal, total) {
			this.Model.pager.params = {
				pageIndex,
				pageSize
			};
			this.Model.pager.total = total;
		},
		cancelInSale(id) {
			this.$confirm('是否确认取消售中?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				this.$ajax
					.get(this.apiTest + `/sale/onSale/order/sale/cancel/${id}`)
					.then(response => {
						const data = response.data;
						if (data.status == 200 && data.result == 1) {
							this.$message({
								type: 'success',
								message: '已成功'
							});
							this.requestTableList();
						};
					})
					.catch(error => {
						this.$message.error('请求错误');
					});
			}).catch(() => {

			});

		},
		cancelRefund(id) {
			this.$confirm('是否确认取消退款?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				this.$ajax
					.get(this.apiTest + `/sale/onSale/order/refund/cancel/${id}`)
					.then(response => {
						const data = response.data;
						if (data.status == 200 && data.result == 1) {
							this.$message({
								type: 'success',
								message: '已成功'
							});
							this.requestTableList();
						};
					})
					.catch(error => {
						this.$message.error('请求错误');
					});
			}).catch(() => {

			});

		},
		sendlRefund(id) {
			this.$ajax
				.get(this.apiTest + `/sale/onSale/order/refund/Initiate/${id}`)
				.then(response => {
					const data = response.data;
					if (data.status == 200 && data.result == 1) {
						this.$message({
							type: 'success',
							message: '已成功'
						});
					}
				})
				.catch(error => {
					this.$message.error('请求错误');
				});
		},
		showPayDialog(id) {
			this.Model.dialogFormVisible = true;
			this.Model.nowSelectId = id;
		},
		pay() {
			const form = this.Model.dialogForm;
			const params = {
				payMethod: form.payMethod,
				paymentMoney: form.paymentMoney,
				serialNumber: form.serialNumber,
				payTime: getDateString(form.payTime),
				orderId: this.Model.nowSelectId
			};
			this.$ajax
				.post(this.apiTest + `/sale/onSale/order/afterSale/pay`, params)
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.$message({
							type: 'success',
							message: '已成功'
						});
						this.requestTableList();
					};
					this.Model.dialogFormVisible = false;
				})
				.catch(error => {
					this.$message.error('请求错误');
					this.Model.dialogFormVisible = false;
				});
		},
	}
}
export default Controller;
